#include "widget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSpacerItem>
#include <QScrollBar>
#include <QEvent>
#include "iconhelper.h"

static QString colors[7] = {"red", "orange", "yellow", "green", "cyan", "blue", "purple"};

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowTitle("图标字体详解");
    // 整体采用垂直布局
    QVBoxLayout* vLayout = new QVBoxLayout(this);
    vLayout->setSpacing(10);
    vLayout->setContentsMargins(10, 10, 10, 10);

    // 1.顶部栏
    QWidget* topWidget = new QWidget(this);
    QHBoxLayout* topLayout = new QHBoxLayout(topWidget);
    topLayout->setSpacing(30);
    topLayout->setContentsMargins(10, 10, 10, 10);

    // 1.1字体库选择
    QGroupBox* fontGroup = new QGroupBox(topWidget);
    fontGroup->setTitle("图标字体库");
    fontGroup->setStyleSheet("font: 18px");
    QVBoxLayout* fontLayout = new QVBoxLayout(fontGroup);

    QString radioText[3] = {"FontAwesome4", "FontWeather", "FontAlibaba"};
    _buttonGroup = new QButtonGroup(fontGroup);
    for (int i = 0; i < 3; ++i) {
        QRadioButton* radioButton = new QRadioButton(fontGroup);
        radioButton->setText(radioText[i]);
        radioButton->setMinimumWidth(150);
        _buttonGroup->addButton(radioButton, i);
        fontLayout->addWidget(radioButton);
        connect(radioButton, &QRadioButton::clicked, this, &Widget::onRadioButtonClicked);
    }
    topLayout->addWidget(fontGroup);

    // 1.2效果显示
    QGroupBox* showGroup = new QGroupBox(topWidget);
    showGroup->setTitle("效果演示");
    showGroup->setObjectName("showGroup");
    // 防止影响子控件的大小
    showGroup->setStyleSheet("QGroupBox#showGroup {font: 18px}");
    QHBoxLayout* showLayout = new QHBoxLayout(showGroup);
    showLayout->setSpacing(10);

    for (int i = 0; i < 7; ++i) {
        QLabel* label = new QLabel(showGroup);
        label->setMinimumSize(100, 100);
        label->setAlignment(Qt::AlignCenter);
        label->setStyleSheet("color: " + colors[i]);
        showLayout->addWidget(label);
        _lblList.append(label);
    }
    topLayout->addWidget(showGroup);

    // 1.3添加弹簧
    QSpacerItem* spacer1 = new QSpacerItem(8, 20, QSizePolicy::Expanding, QSizePolicy::Fixed);
    topLayout->addSpacerItem(spacer1);

    // 2.主体
    QScrollArea* scrollArea = new QScrollArea(this);
    scrollArea->setWidgetResizable(true);
    scrollArea->setMinimumHeight(400);

    QWidget* scrollAreaContent = new QWidget(this);

    QVBoxLayout* scrollLayout = new QVBoxLayout(scrollAreaContent);
    scrollLayout->setSpacing(0);
    scrollLayout->setContentsMargins(10, 10, 10, 10);

    // 给scrollArea设置widget
    scrollArea->setWidget(scrollAreaContent);

    QFrame* frame = new QFrame(scrollAreaContent);
    _gridLayout = new QGridLayout(frame);
    _gridLayout->setSpacing(0);
    _gridLayout->setContentsMargins(10, 10, 10, 10);
    scrollLayout->addWidget(frame);

    QSpacerItem* spacer2 = new QSpacerItem(8, 20, QSizePolicy::Fixed, QSizePolicy::Expanding);
    scrollLayout->addSpacerItem(spacer2);

    vLayout->addWidget(topWidget);
    vLayout->addWidget(scrollArea);

    _buttonGroup->button(0)->click();
}

Widget::~Widget() {}

bool Widget::eventFilter(QObject* obj, QEvent* event)
{
    if (event->type() == QEvent::Enter) {
        if (typeid(QLabel) == typeid(*obj)) {
            QLabel* label = dynamic_cast<QLabel*>(obj);
            int icon = label->toolTip().toInt(nullptr, 16);
            int checkId = _buttonGroup->checkedId();
            QString iconName;
            switch (checkId) {
            case 0:
                iconName = FONT_FAMILY_AWESOME4;
                break;
            case 1:
                iconName = FONT_FAMILY_WEATHER;
                break;
            case 2:
                iconName = FONT_FAMILY_ALIBABA;
                break;
            default:
                break;
            }
            for (int i = 0; i < _lblList.size(); ++i) {
                IconHelper::GetInstance()->setIcon(_lblList[i], iconName, icon, 92);
            }
        }
    }
}

void Widget::onRadioButtonClicked()
{
    // gride中有多少控件
    int count = _gridLayout->count();
    // 释放原有控件
    while (count--) {
        QWidget* widget = _gridLayout->takeAt(count)->widget();
        _gridLayout->removeWidget(widget);
        widget->setVisible(false);
        widget->deleteLater();
    }

    int checkId = _buttonGroup->checkedId();
    QList<int> list;
    QString fontFamily;
    switch (checkId) {
    case 0:
        fontFamily = FONT_FAMILY_AWESOME4;
        IconHelper::GetInstance()->getAwesome4Icons(list);
        break;
    case 1:
        fontFamily = FONT_FAMILY_WEATHER;
        IconHelper::GetInstance()->getWeatherIcons(list);
        break;
    case 2:
        fontFamily = FONT_FAMILY_ALIBABA;
        IconHelper::GetInstance()->getAlibabaIcons(list);
        break;
    default:
        break;
    }

    int row, column = 0;
    for (int i = 0; i < list.size(); ++i) {
        QLabel* label = new QLabel(this);
        label->setMinimumSize(48, 48);
        label->setAlignment(Qt::AlignCenter);
        label->setStyleSheet(R"(
            QLabel { color: #000000; border:1px solid #000000; background-color: rgb(255,255,255);}
            QLabel:hover { color: #ffffff; border:1px solid #000000; background-color: rgb(0,0,0);}
        )");

        // 能触发eventFilter事件
        label->installEventFilter(this);

        label->setToolTip("0x" + QString::number(list[i], 16));
        IconHelper::GetInstance()->setIcon(label, fontFamily, list[i], 40);

        // 每行显示20个
        row = i / 20;
        column = i % 20;
        _gridLayout->addWidget(label, row, column);
    }
}
